//
//  ServiceCell.m
//  mendianjieke
//
//  Created by 响当当 on 7/31/19.
//  Copyright © 2019 dongzheng. All rights reserved.
//

#import "ServiceCell.h"
#import "CustomerInfoViewController.h"

@interface ServiceCell()

@property (nonatomic, strong)UIImageView *expressionImageView;//表情
@property (nonatomic, strong)UIImageView *headImage;//头像
@property (nonatomic, strong)UILabel *subTitleLabel;//副标题
@property (nonatomic, strong)UIImageView *sexImageView;//性别头像
@property (nonatomic, strong)UIButton *normalButton;//基本状态

@property (nonatomic, strong)UIImageView *headImageView;//头像
@property (nonatomic, strong)UIButton *checkAttackButton;//查看攻略
@property (nonatomic, strong)UIButton *overButton;//立即结束
@property (nonatomic, assign)NSInteger bageNum;
@end

@implementation ServiceCell

- (UIImageView *)expressionImageView
{
    if (!_expressionImageView)
    {
        _expressionImageView = [[UIImageView alloc]initWithFrame:CGRectMake(15*kUIScaleBasePlus,19.5*kUIScaleBasePlus,17.5*kUIScaleBasePlus,17.5*kUIScaleBasePlus)];
        _expressionImageView.layer.masksToBounds = YES;
        _expressionImageView.layer.cornerRadius = _headImage.frame.size.width/2;
//        _expressionImageView.backgroundColor = JSAMainColor;
    }
    return _expressionImageView;
}

- (UIImageView *)headImageView
{
    if (!_headImageView)
    {
        UIImageView *bgImageView = [[UIImageView alloc]initWithFrame:CGRectMake(15*kUIScaleBasePlus,CGRectGetMaxY(self.expressionImageView.frame)+20*kUIScaleBasePlus,110*kUIScaleBasePlus,110*kUIScaleBasePlus)];
        bgImageView.image = [UIImage imageNamed:@"photoBG"];
        [self.contentView addSubview:bgImageView];
        
        _headImageView = [[UIImageView alloc]initWithFrame:CGRectMake(20*kUIScaleBasePlus,CGRectGetMaxY(self.expressionImageView.frame)+25*kUIScaleBasePlus,100*kUIScaleBasePlus,100*kUIScaleBasePlus)];
    }
    return _headImageView;
}

- (UILabel *)subTitleLabel
{
    if (!_subTitleLabel)
    {
        _subTitleLabel = [Utilties creatLabelWithFrame:CGRectMake(CGRectGetMaxX(self.expressionImageView.frame)+3.5*kUIScaleBasePlus,23*kUIScaleBasePlus,40*kUIScaleBasePlus,10.5*kUIScaleBasePlus)
                                          textAligment:NSTextAlignmentLeft
                                              textClor:LCHexColor(0x000000)
                                              textFont:11*kUIScaleBasePlus];;
    }
    return _subTitleLabel;
}

- (UIImageView *)sexImageView
{
    if (!_sexImageView)
    {
        _sexImageView = [[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.subTitleLabel.frame)+21*kUIScaleBasePlus,20.5*kUIScaleBasePlus,15*kUIScaleBasePlus,15*kUIScaleBasePlus)];
    }
    return _sexImageView;
}

- (UIButton *)normalButton
{
    if (!_normalButton)
    {
        _normalButton = [Utilties creatButtonWithFrame:CGRectMake(CGRectGetMaxX(self.sexImageView.frame)+5*kUIScaleBasePlus,22.5*kUIScaleBasePlus,75*kUIScaleBasePlus,11.5*kUIScaleBasePlus)
                                          textAligment:UIControlContentHorizontalAlignmentLeft
                                                  text:@"性别"
                                              textClor:LCHexColor(0x282828)
                                             buttonBGC:[UIColor clearColor]
                                              textFont:11*kUIScaleBasePlus
                                                target:self
                                                method:@selector(normalClcik)];
        _normalButton.enabled = NO;
    }
    return _normalButton;
}

- (UIButton *)checkAttackButton
{
    if (!_checkAttackButton)
    {
        _checkAttackButton = [Utilties creatButtonWithFrame:CGRectMake(CGRectGetMaxX(self.headImageView.frame)+40*kUIScaleBasePlus,63.5*kUIScaleBasePlus,140*kUIScaleBasePlus, 36*kUIScaleBasePlus)
                                               textAligment:UIControlContentHorizontalAlignmentCenter
                                                       text:@"查看攻略"
                                                   textClor:LCHexColor(0xFFFFFF)
                                                  buttonBGC:LCHexColor(0x05D1A1)
                                                   textFont:18*kUIScaleBasePlus
                                                     target:self
                                                     method:@selector(checkAttackClick)];
        _checkAttackButton.layer.masksToBounds = YES;
        _checkAttackButton.layer.cornerRadius = _checkAttackButton.frame.size.height/2;
    }
    return _checkAttackButton;
}

- (UIButton *)overButton
{
    if (!_overButton)
    {
        _overButton = [Utilties creatButtonWithFrame:CGRectMake(CGRectGetMaxX(self.headImageView.frame)+40*kUIScaleBasePlus,CGRectGetMaxY(self.checkAttackButton.frame)+14*kUIScaleBasePlus,140*kUIScaleBasePlus, 36*kUIScaleBasePlus)
                                               textAligment:UIControlContentHorizontalAlignmentCenter
                                                       text:@"立即结束"
                                                   textClor:LCHexColor(0xFFFFFF)
                                                  buttonBGC:LCHexColor(0xFF7D7D)
                                                   textFont:18*kUIScaleBasePlus
                                                     target:self
                                                     method:@selector(overButtonClick)];
        _overButton.layer.masksToBounds = YES;
        _overButton.layer.cornerRadius = _overButton.frame.size.height/2;
    }
    return _overButton;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = 10;
        
        NSString *str = LCUserDefaultsGet(@"bageNum");
        self.bageNum = str.integerValue;
        [self.contentView addSubview:self.expressionImageView];
        [self.contentView addSubview:self.subTitleLabel];
        [self.contentView addSubview:self.sexImageView];
        [self.contentView addSubview:self.normalButton];
        [self.contentView addSubview:self.headImageView];
        [self.contentView addSubview:self.checkAttackButton];
        [self.contentView addSubview:self.overButton];
    }
    return self;
}

#pragma mark - Method
- (void)checkAttackClick
{
    CustomerInfoViewController *vc = [[CustomerInfoViewController alloc]init];
    vc.hidesBottomBarWhenPushed = YES;
    vc.customerID = self.model.customerId;
    [[Utilties getCurrentVC].navigationController pushViewController:vc animated:YES];
}

- (void)overButtonClick
{
    NSDictionary *pramaters = @{
                                @"id" : self.model.customerId,
                                @"operate" : @"2"
                                };
    
    [SVProgressHUD showWithStatus:@"正在加载中"];
    self.overButton.enabled = NO;
    WeakSelf(self)
    [infoManager cancelCustomer:pramaters sucess:^(id  _Nonnull acceptData) {
        NSNumber *num = acceptData[@"data"];
        self.overButton.enabled = YES;
        if (num.integerValue == 0)
        {
            if (weakSelf.serviceLockBlock)
            {
                NSString *str = LCUserDefaultsGet(@"bageNum");
                self.bageNum = str.integerValue;
                self.bageNum--;
                NSString *bageNum = [NSString stringWithFormat:@"%ld",self.bageNum];
                LCUserDefaultsSet(bageNum, @"bageNum");
                LCUserDefaultsSyn;
                AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
                [appDelegate showTabCount:[NSString stringWithFormat:@"%ld",self.bageNum]];
                weakSelf.serviceLockBlock();
            }
        }
    } fail:^(NSError * _Nullable error) {
        [SVProgressHUD dismiss];
        self.overButton.enabled = YES;
    }];
}

- (void)setModel:(RealTimeModel *)model
{
    _model = model;
    
    SummaryModel *smodel = [[SummaryModel alloc]initWithDic:model.summary];
       
       if (smodel.gender.intValue == 0)
       {
           self.sexImageView.image = [UIImage imageNamed:@"sex_gril"];
       }
       else
       {
           self.sexImageView.image = [UIImage imageNamed:@"sex_boy"];
       }
       
    [self.normalButton setTitle:[NSString stringWithFormat:@"%ld，%@",smodel.age.integerValue,smodel.gender.integerValue == 0 ? @"女" : @"男"] forState:UIControlStateNormal];
    
    self.subTitleLabel.text = model.customerType.integerValue == 1 ? @"新顾客" : @"老顾客";
      if (model.customerType.integerValue == 1)
      {
          self.subTitleLabel.textColor = LCHexColor(0x015AE6);
      }
      else
      {
          self.subTitleLabel.textColor = LCHexColor(0xE3413F);
      }
    
    [self.headImageView sd_setImageWithURL:[NSURL URLWithString:model.photo] placeholderImage:[UIImage imageNamed:@"zhanwei"]];
    
    
    switch (model.expression.integerValue)
         {
             case 0:
             {
                 self.expressionImageView.image = [UIImage imageNamed:@"jingya"];
             }
                     break;
             case 3:
             {
                 self.expressionImageView.image = [UIImage imageNamed:@"kaixin"];
                 
             }
                 break;
             case 4:
             {
                 self.expressionImageView.image = [UIImage imageNamed:@"beishang"];
                 
             }
                 break;
             case 5:
             {
                 self.expressionImageView.image = [UIImage imageNamed:@"shengqi"];
                 
             }
                 break;
             case 6:
             {
                 self.expressionImageView.image = [UIImage imageNamed:@"pingjing"];
                 
             }
                 break;
                 default:
                     break;
         }

}
@end
